﻿using IBLL;
using IBLL.Dto;
using IDAL;
using Model;

namespace BLL
{
    public class HouseNumBLL : IHouseNumBLL
    {
        public HouseNumBLL(IBaseDAL<HouseNum> db)
        {
            Db = db;
        }

        public IBaseDAL<HouseNum> Db { get; }
        /// <summary>
        /// 房号添加信息
        /// </summary>
        /// <param name="houseNum">房号实体对象</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> AddAsync(HouseNum houseNum)
        {
           return await Db.AddAsync(houseNum);
        }
        /// <summary>
        /// 根据编号id删除一条信息
        /// </summary>
        /// <param name="Id">编号id</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> DeleteAsync(int Id)
        {
          return await Db.DeleteAsync(Id);
        }
        /// <summary>
        /// 分页查询信息
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="size">页容量</param>
        /// <param name="name">房型名称模糊查询</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public GetPageData GetHousePage(int index, int size, string? name)
        {
            var m = Db.GetAll().AsQueryable();
            if(!string.IsNullOrEmpty(name))
            {
                m = m.Where(x => x.Hname.Contains(name));
            }
            var c = m.Count();
            var d = m.OrderBy(x => x.Id).Skip((index - 1) * size).Take(size).ToList();

            GetPageData dto = new GetPageData();
            dto.Count = c;
            dto.Data = d;
            return dto;

        }

        /// <summary>
        /// 根据编号id查询一条信息
        /// </summary>
        /// <param name="Id">编号id</param>
        /// <returns></returns>
        public  HouseNum GetValue(int Id)
        {
           return  Db.GetValue(Id);
        }
        /// <summary>
        /// 逻辑删除 根据编号id，修改其是否删除状态
        /// </summary>
        /// <param name="Id">编号id</param>
        /// <returns></returns>
        public async Task<int> LogicDelete(int Id)
        {
           HouseNum s=Db.GetValue(Id);
            s.IsDelete = true;
            return await Db.UpdateAsync(s);
        }
        /// <summary>
        /// 修改房号信息
        /// </summary>
        /// <param name="houseNum">房号实体对象</param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(HouseNum houseNum)
        {
            return await Db.UpdateAsync(houseNum);
        }
    }
}
